package com.turing.post.thirdinterface.impl;

import com.turing.post.util.PCPSUtil;

/**
 * 燃气加气卡管理信息(A00000000386980703/0016)
 * 
 * @author yxb
 * 
 */
public class GasCardManagementInf {
	/**
	 * 01－01 版本(当前版本: 0x01) 1 BCD
	 */
	private String version;

	/**
	 * 初始版本01
	 */
	public static final String VERSION_01 = "01";

	/**
	 * 空白区
	 */
	public static final String VERSION_00 = "00";

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	/**
	 * 02－31 气瓶编号 30 ASC
	 */
	private String gasBottleNo;

	public String getGasBottleNo() {
		return gasBottleNo;
	}

	public void setGasBottleNo(String gasBottleNo) {
		this.gasBottleNo = gasBottleNo;
	}

	/**
	 * 32－61 车架号 30 ASC
	 */
	private String vin;

	public String getVin() {
		return vin;
	}

	public void setVin(String vin) {
		this.vin = vin;
	}

	/**
	 * 62－71 注册编号 10 ASC
	 */
	private String regNo;

	public String getRegNo() {
		return regNo;
	}

	public void setRegNo(String regNo) {
		this.regNo = regNo;
	}

	/**
	 * 72－73 车种类 2 ASC
	 */
	private String vehicleType;

	public String getVehicleType() {
		return vehicleType;
	}

	public void setVehicleType(String vehicleType) {
		this.vehicleType = vehicleType;
	}

	/**
	 * 74－85 车牌号 12 ASC
	 */
	private String vehicleNo;

	public String getVehicleNo() {
		return vehicleNo;
	}

	public void setVehicleNo(String vehicleNo) {
		this.vehicleNo = vehicleNo;
	}

	/**
	 * 86－89 到检日期 4 BCD
	 */
	private String expired;

	public String getExpired() {
		return expired;
	}

	public void setExpired(String expired) {
		this.expired = expired;
	}

	/**
	 * 90－90 气瓶数量 1 BCD
	 */
	private int gasBottleCount;

	public int getGasBottleCount() {
		return gasBottleCount;
	}

	public void setGasBottleCount(int gasBottleCount) {
		this.gasBottleCount = gasBottleCount;
	}

	/**
	 * 91－91 状态标志 1 BCD(01-正常 其他-未定义)
	 */
	private String status;

	public static final String STATUS_UNISSUE = "00";
	public static final String STATUS_NORMAL = "01";

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * 92－151 车主名称 60 ASC
	 */
	private String owner;

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public String getOwner() {
		return owner;
	}

	/**
	 * 152－211 RFU 60 HEX
	 */

	/**
	 * 判定管理区是否为可发行区
	 */
	public boolean maybeIssue() {
		if (PCPSUtil.compareObject(STATUS_UNISSUE, getStatus())
				&& PCPSUtil.compareObject(VERSION_00, getVersion()))
			return true;

		return false;
	}

	/**
	 * 将燃气加气卡管理信息编码成16进制字符串以准备写卡
	 * 
	 * @return
	 */
	public String encodeGasCardManagementInf() {
		StringBuilder sb = new StringBuilder();
		// 此处应检查数据的合法性
		// 版本号
		sb.append(VERSION_01);
		// 气瓶编号
		sb.append(PCPSUtil.encodeAscToHexString(getGasBottleNo(), 30));
		// 车架号
		sb.append(PCPSUtil.encodeAscToHexString(getVin(), 30));
		// 注册编号
		sb.append(PCPSUtil.encodeAscToHexString(getRegNo(), 10));
		// 车种类
		sb.append(PCPSUtil.encodeAscToHexString(getVehicleType(), 2));
		// 车牌号
		sb.append(PCPSUtil.encodeAscToHexString(getVehicleNo(), 12));
		// 到检日期
		sb.append(getExpired() == null ? "00000000" : getExpired());
		// 气瓶数量
		sb.append(PCPSUtil.getByteHexString(getGasBottleCount()));
		// 状态标志
		sb.append(getStatus());
		// 车主名称
		sb.append(PCPSUtil.encodeAscToHexString(getOwner(), 60));
		// RFU
		sb.append(PCPSUtil.encodeAscToHexString("", 60));
		return sb.toString();
	}

	/**
	 * 解析HEX字符串内容到燃气加气卡管理信息对象
	 * 
	 * @param hexstr
	 * @return
	 */
	public static GasCardManagementInf parseGasCardManagementInf(String hexstr) {
		if (PCPSUtil.isEmpty(hexstr))
			return null;

		if (hexstr.length() < 151 * 2)
			return null;

		int off = 0;
		GasCardManagementInf cir = new GasCardManagementInf();

		// 解析version
		cir.setVersion(hexstr.substring(0, 2));
		off += 2;

		// 解析气瓶编号
		cir.setGasBottleNo(PCPSUtil.decodeHexStringToAsc(hexstr, off, 30 * 2));
		off += 30 * 2;

		// 解析车架号
		cir.setVin(PCPSUtil.decodeHexStringToAsc(hexstr, off, 30 * 2));
		off += 30 * 2;

		// 注册编号
		cir.setRegNo(PCPSUtil.decodeHexStringToAsc(hexstr, off, 10 * 2));
		off += 10 * 2;

		// 车种类
		cir.setVehicleType(PCPSUtil.decodeHexStringToAsc(hexstr, off, 2 * 2));
		off += 2 * 2;

		// 车牌号
		cir.setVehicleNo(PCPSUtil.decodeHexStringToAsc(hexstr, off, 12 * 2));
		off += 12 * 2;

		// 到检日期
		cir.setExpired(hexstr.substring(off, off + 4 * 2));
		off += 4 * 2;

		// 气瓶数量
		byte[] bs = PCPSUtil.getBytes(hexstr.substring(off, off + 2));
		cir.setGasBottleCount(PCPSUtil.parseNetworkInteger(bs, 0, 1));
		off += 2;

		// 状态标志
		cir.setStatus(hexstr.substring(off, off + 2));
		off += 2;

		// 车主名称
		cir.setOwner(PCPSUtil.decodeHexStringToAsc(hexstr, off, 60 * 2));

		return cir;
	}
	
	public static void main(String[] args) throws Exception {
		GasCardManagementInf gcmi = new GasCardManagementInf();
		gcmi.setOwner("测试车主");
		gcmi.setVehicleNo("陕O12345");
		gcmi.setVin("LSG");
		gcmi.setStatus(STATUS_NORMAL);
		gcmi.setExpired("20151231");
		System.out.println(gcmi.encodeGasCardManagementInf());
	}
}
